package com.order.controller.admin;


import com.order.constant.JwtClaimsConstant;
import com.order.dto.AdminLoginDTO;
import com.order.dto.UserLoginDTO;
import com.order.entity.Admin;
import com.order.entity.User;
import com.order.properties.JwtProperties;
import com.order.result.Result;
import com.order.service.AdminService;
import com.order.utils.JwtUtil;
import com.order.vo.AdminLoginVO;
import com.order.vo.UserLoginVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin/admin")
@Slf4j
@Api(tags = "商家登录接口")
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    public Result<AdminLoginVO> login(@RequestBody AdminLoginDTO adminLoginDTO){
        log.info("商家登录：{}", adminLoginDTO);
        Admin admin = adminService.login(adminLoginDTO);

        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, admin.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        AdminLoginVO adminLoginVO = AdminLoginVO.builder()
                .id(admin.getId())
                .userName(admin.getUsername())
                .token(token)
                .build();

        return Result.success(adminLoginVO);

    }
}
